<template>
  <el-drawer v-model="drawer" @closed="closed">
    <!-- 标题部分 -->
    <template #header>
      <h4>新增</h4>
    </template>
    <template #default>
      <el-form
        ref="form"
        :model="formParams"
        :rules="rules"
        label-width="100px"
      >
          <el-form-item :label="$t('boat.model')" prop="vesselsId">
            <el-select
              v-model="formParams.vesselsId"
              :placeholder="$t('operation.select')"
              size="large"
              style="width: 220px;"
              filterable
              @change="change"
            >
              <el-option
                v-for="item in list"
                :key="item.id"
                :label="item.model"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('boat.user')" prop="customerId">
            <el-select
              v-model="formParams.customerId"
              :placeholder="$t('operation.select')"
              size="large"
              style="width: 220px;"
              filterable
              @change="change"
            >
              <el-option
                v-for="item in members"
                :key="item.id"
                :label="item.customerFirstName + ' ' + item.customerLastName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>

        <el-form-item :label="$t('boat.saleMoney')" prop="saleAmount">
          <el-input-number :placeholder="$t('boat.saleMoney')" v-model="formParams.saleAmount" :min="1" />
        </el-form-item>
        <el-form-item :label="$t('boat.saleTime')" prop="saleDate">
          <el-date-picker
            v-model="formParams.saleDate"
            type="datetime"
            :placeholder="$t('boat.saleTime')"
            format="DD/MM/YYYY HH:mm:ss"
            value-format="DD/MM/YYYY HH:mm:ss"
          />
        </el-form-item>
        <el-form-item :label="$t('boat.saleMan')" prop="salePerson">
          <el-input
            :placeholder="$t('boat.saleMan')"
            v-model="formParams.salePerson"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('boat.salePlace')" prop="salePlace">
          <el-input
            :placeholder="$t('boat.salePlace')"
            v-model="formParams.salePlace"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('boat.saleNum')" prop="saleNum">
          <el-input-number :placeholder="$t('boat.saleNum')" v-model="formParams.saleNum" :min="1" />
        </el-form-item>
        <!-- <el-form-item label="图标">
                    <el-input placeholder="图标" v-model="formParams.icon"></el-input>
                </el-form-item> -->
        <el-form-item :label="$t('boat.des')">
          <el-input
            type="textarea"
            :placeholder="$t('boat.des')"
            v-model="formParams.depict"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            size="default"
            @click="save"
          >
            保存
          </el-button>
          <el-button type="primary" size="default" @click="drawer = false">
            取消
          </el-button>
        </el-form-item>
      </el-form>
    </template>
  </el-drawer>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue'
import { saleAdd, saleEdit } from '@/api/boat/sale'
import { ElMessage } from 'element-plus'
import { typeArr } from './type'
import { vesselsGetAll } from '@/api/boat/basic'
import { customerGetAll } from '@/api/seek_member/member'

let props = defineProps(['list']) //数组|对象写法都可以

onMounted(async () => {
  getBoats()
  getMembers()
})

let list = ref([])
const getBoats = async () => {
  let { status, data, msg } = await vesselsGetAll()
  if (status == 200) {
    list.value = data
  } else {
    ElMessage({ type: 'error', message: msg })
  }
}

let members = ref([])
const getMembers = async () => {
  let { status, data, msg } = await customerGetAll()
  if (status == 200) {
    members.value = data
  } else {
    ElMessage({ type: 'error', message: msg })
  }
}

const defaultForm = {
  id: '',
  vesselsId: '',
  customerId: '',
  saleAmount: '',
  saleDate: '',
  salePerson: '',
  salePlace: '',
  saleNum: '',
  depict: '',
}

let formParams = reactive<any>({
  ...defaultForm,
})
const rules = {
  vesselsId: [{ required: true, message: '请选择', trigger: 'blur' }],
  customerId: [{ required: true, message: '请选择', trigger: 'blur' }],
  maintainAmount: [{ required: true, message: '请输入', trigger: 'blur' }],
  // maintainDate: [
  //   { required: true, type: 'number', message: '请选择', trigger: 'blur' },
  // ],
  vesselsCode: [{ required: true, message: '请输入', trigger: 'blur' }],
}

let drawer = ref(false)
let edit = ref(false)

const show = (row) => {
  if (row) {
    edit.value = true

    Object.keys(formParams).forEach((key) => {
      formParams[key] = row[key]
    })
    if (!formParams.parentId) {
      formParams.parentId = ''
    }
  } else {
    edit.value = false
  }
  drawer.value = true
}

let loading = ref(false)
let form = ref()
//保存按钮的方法
const save = async () => {
  await form.value.validate()
  loading.value = true
  let result: any
  
  if (edit.value) {
    result = await saleEdit(formParams)
  } else {
    result = await saleAdd(formParams)
  }
  loading.value = false
  if (result.status == 200) {
    ElMessage({
      type: 'success',
      message: '操作成功',
    })
    drawer.value = false
    $emit('callback')
  } else {
    ElMessage({
      type: 'error',
      message: '操作失败',
    })
  }
}

const closed = () => {
  Object.assign(formParams, defaultForm)
}

const update = (e) => {
  formParams.url = e
}

const treeSelect = (e) => {
  // formParams.parentId = e.permissionId
  // console.log(formParams.parentId);
}

//组件内部数据对外关闭的，别人不能访问
//如果想让外部访问需要通过defineExpose方法对外暴露
defineExpose({
  show,
})
//自定义事件的方法
let $emit = defineEmits(['callback'])
</script>
